package fx.mine.active.component.music;

import animatefx.animation.FadeIn;
import fx.mine.active.util.GlobalConfigUtils;
import javafx.beans.binding.Bindings;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

@Log4j2
public class SliderContainer extends AnchorPane {
    private static final Timer TIMER = new Timer();

    public static Timer getExecutors() {
        return TIMER;
    }

    public SliderContainer() {
        AnchorPane.setLeftAnchor(this, 50.0);
        AnchorPane.setRightAnchor(this, 50.0);
        AnchorPane.setTopAnchor(this, 10.0);

        setPrefHeight(250);
        setMaxHeight(getPrefHeight());
        setMaxWidth(getPrefWidth());
        log.info("prefWidth = {} , prefHeight = {} , width = {} , height = {}, maxWidth = {}, maxHeight = {}",
                getPrefWidth(), getPrefHeight(), getWidth(), getHeight(), getMaxWidth(), getMaxHeight());
        initialSilider();
        getStyleClass().add("sliderContainer");
        getStylesheets().add(getClass().getResource("/css/sliderContainer.css").toExternalForm());
    }

    private void initialSilider() {
        ObservableList<Node> children = getChildren();

        AnchorPane pane1 = new AnchorPane();
        pane1.setPrefWidth(750);
        AnchorPane.setLeftAnchor(pane1, 0.0);
        AnchorPane.setTopAnchor(pane1, 25.0);
        AnchorPane.setBottomAnchor(pane1, 25.0);
        pane1.getStyleClass().add("pane1");


        AnchorPane pane2 = new AnchorPane();
        pane2.setPrefWidth(700);
        AnchorPane.setLeftAnchor(pane2, 115.0);
        AnchorPane.setTopAnchor(pane2, 0.0);
        AnchorPane.setBottomAnchor(pane2, 0.0);
        pane2.getStyleClass().add("pane2");

        AnchorPane pane3 = new AnchorPane();
        pane3.setPrefWidth(750);
        AnchorPane.setRightAnchor(pane3, 0.0);
        AnchorPane.setTopAnchor(pane3, 25.0);
        AnchorPane.setBottomAnchor(pane3, 25.0);

        pane3.setPrefSize(750, 200);
        pane3.getStyleClass().add("pane3");

        children.add(pane1);
        children.add(pane3);
        children.add(pane2);

        ImageView imageView1 = new ImageView("http://image.wufazhuce.com/FuLThNJCQOvKb-_z_VrBrW3RHQAG");
        imageView1.setManaged(true);
        imageView1.setFitWidth(750);
        imageView1.setFitHeight(200);
        pane1.getChildren().add(imageView1);

        ImageView imageView2 = new ImageView("http://image.wufazhuce.com/FiEC0TICqvNrPgYOmxW7GLBnleVa");
        imageView2.setFitWidth(700);
        imageView2.setFitHeight(250);
        pane2.getChildren().add(imageView2);

//        ImageView imageView3 = new ImageView("http://image.wufazhuce.com/FqtEw3pMuK1H8ZprlwxSZZU_GXY8");
        ImageView imageView3 = new ImageView("http://image.wufazhuce.com/FokffjwghcHIMQSpqVb68XutIiO9");
        imageView3.setFitWidth(750);
        imageView3.setFitHeight(200);
        pane3.getChildren().add(imageView3);

        EventHandler<MouseEvent> e = event -> {
            play(pane1, pane2, pane3);
        };

        pane1.setOnMouseClicked(e);
        pane3.setOnMouseClicked(e);
        if (GlobalConfigUtils.CONFIG_BEAN.isAutoSlide()) {
            autoSlide(pane1, pane2, pane3);
        }
    }

    private void autoSlide(AnchorPane pane1, AnchorPane pane2, AnchorPane pane3) {
        SliderTask sliderTask = new SliderTask(pane1, pane2, pane3);
        TIMER.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    sliderTask.call();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }, 10000, 2000);
    }

    private static void play(AnchorPane pane1, AnchorPane pane2, AnchorPane pane3) {
        FadeIn fadeOutLeft = new FadeIn();
        Node node2 = pane2.getChildren().get(0);
        fadeOutLeft.setNode(node2);

        fadeOutLeft.setOnFinished(finishEvent -> {
            slide(pane1, pane2, pane3, node2);
        });
        fadeOutLeft.play();
    }

    private static void slide(AnchorPane pane1, AnchorPane pane2, AnchorPane pane3, Node node2) {
        Node node1 = pane1.getChildren().get(0);
        Node node3 = pane3.getChildren().get(0);

        pane1.getChildren().clear();
        ((ImageView) node2).setFitHeight(200);
        ((ImageView) node2).setFitWidth(750);
        pane1.getChildren().add(node2);
        pane1.applyCss();

        pane2.getChildren().clear();
        ((ImageView) node3).setFitWidth(700);
        ((ImageView) node3).setFitHeight(250);
        pane2.getChildren().add(node3);

        pane3.getChildren().clear();
        ((ImageView) node1).setFitWidth(750);
        ((ImageView) node1).setFitHeight(200);
        pane3.getChildren().add(node1);
    }

    @AllArgsConstructor
    class SliderTask extends Task<Void> {

        private AnchorPane pane1;
        private AnchorPane pane2;
        private AnchorPane pane3;

        @Override
        protected Void call() throws Exception {
            log.info("start schedule {}", new Date());
            play(pane1, pane2, pane3);
            return null;
        }

    }


}
